﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using esui_mvc.Models;
using Newtonsoft.Json;
using EntityState = System.Data.Entity.EntityState;

namespace esui_mvc.Controllers
{
    public class TerritoriesController : Controller
    {
        private esui_mvcContext db = new esui_mvcContext();

        // GET: Territories
        public ActionResult Index(FormCollection formCollection)
        {
            int page = Convert.ToInt32(formCollection["page"]);
            int row = Convert.ToInt32(formCollection["rows"]);

            #region 注释
            /*EasyUI的DataGrid要求返回的JSON数据集是这样的形式：
【“total”:总记录数量 , "rows":数据记录数组】
例如：
{"total":100,"rows":[{"name":"张三","id":1},{"name":"李四","id":2}]}
★需要注意的是：
1.返回结果必须是JSON格式而不是普通字符串。
2.注意JSON字符串中的单双引号转义。
【建议】在服务端Java代码中使用Jackson或者JSON-lib来创建JSON对象并赋值，然后再toString转为JSON字符串返回到前端页面。 */
            
            #endregion
            int total = db.Territories.Count();
            var tt = db.Regions.ToList();
            var s = db.Territories.ToList().Skip((page - 1) * row).Take(row);
            var data = new
            {
                total,
                rows= from te in s select
                      new
                      {
                          TerritoryID = te.TerritoryID,
                          TerritoryDescription = te.TerritoryDescription,
                          Regions = new
                          {
                              RegionDescription = te.Region.RegionDescription,
                              RegionID = te.RegionID
                          }
                      }
                      
            };
            return Json(data,JsonRequestBehavior.AllowGet);
        }

        // GET: Territories/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Territory territory = db.Territories.Find(id);
            if (territory == null)
            {
                return HttpNotFound();
            }
            return View(territory);
        }

        // GET: Territories/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Territories/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "TerritoryID,TerritoryDescription,RegionID")] Territory territory)
        {
            if (ModelState.IsValid)
            {
                db.Territories.Add(territory);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(territory);
        }

        // GET: Territories/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Territory territory = db.Territories.Find(id);
            if (territory == null)
            {
                return HttpNotFound();
            }
            return View(territory);
        }

        // POST: Territories/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "TerritoryID,TerritoryDescription,RegionID")] Territory territory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(territory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(territory);
        }

        // GET: Territories/Delete/5
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Territory territory = db.Territories.Find(id);
            if (territory == null)
            {
                return HttpNotFound();
            }
            return View(territory);
        }

        // POST: Territories/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            Territory territory = db.Territories.Find(id);
            db.Territories.Remove(territory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        public ActionResult Regions()
        {
            var s = from re in db.Regions
                select new
                {
                    RegionID = re.RegionID,
                    RegionDescription = re.RegionDescription
                };
            return Json(s, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult updata(string TerritoryID, string TerritoryDescription, int RegionID)
        {
            Territory te=  db.Territories.Find(TerritoryID);
            te.TerritoryDescription = TerritoryDescription;
            te.RegionID = RegionID;
            db.Entry(te).State=EntityState.Modified;
            db.SaveChanges();
            return Content("ok");
        }
    }
}
